/*
 * ASTUtil.h
 *
 *  Created on: Nov 19, 2010
 *      Author: dhovemey
 */

#ifndef ASTUTIL_H_
#define ASTUTIL_H_

#include <vector>
#include "fe/ASTNode.h"

namespace ASTUtil {

typedef std::vector<ASTNode*> ASTNodeList;

struct IsClassOrModule {
	bool operator()(ASTNode *astNode) const {
		return astNode->isClassOrModule();
	}
};

struct IsMethodOrMagicMethod {
	bool operator()(ASTNode *astNode) const {
		return astNode->getType() == ASTNode::AST_METHOD || astNode->getType() == ASTNode::AST_MAGIC_METHOD;
	}
};

struct IsField {
	bool operator()(ASTNode *astNode) const {
		return astNode->getType() == ASTNode::AST_FIELD;
	}
};

struct IsMember {
	bool operator()(ASTNode *astNode) const {
		return IsField()(astNode) || IsMethodOrMagicMethod()(astNode);
	}
};

template<class Pred>
void findChildren(ASTNode *astNode, Pred p, ASTNodeList &vec) {
	ASTNode::ChildList &childList = astNode->getChildList();
	for (ASTNode::ChildList::iterator i = childList.begin(); i != childList.end(); i++) {
		ASTNode *child = *i;
		if (p(child)) {
			vec.push_back(child);
		}
	}
}

unsigned getNumFields(ASTNode *clsOrModule);

} // end namespace ASTUtil

#endif /* ASTUTIL_H_ */
